package com.oa.controller;


import com.alibaba.fastjson.JSON;
import com.oa.bean.Dept;
import com.oa.bean.Job;
import com.oa.service.DeptService;
import com.oa.service.JobService;
import com.oa.service.DeptServiceImpl;
import com.oa.service.JobServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//请求接口-数据查询【部门数据-岗位数据】
@WebServlet(urlPatterns = {"/job_DeptList"})
public class IndexController extends HttpServlet {

    //获取业务层接口的对象
    DeptService deptService = new DeptServiceImpl();
    JobService jobService = new JobServiceImpl();

    //doGet方法是用于接收get请求的
    @Override   //          request  请求对象           response  响应对象
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置响应类型为json形式的数据--以及设置响应的字符集编码
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        //定义一个map集合用于保存返回的键值对数据
        Map map = new HashMap();
        //查询部门数据-岗位数据
        List<Dept> deptList = deptService.queryDept();
        List<Job> jobList = jobService.queryJob();
        //保存--应用作用域
        map.put("deptList", deptList);
        map.put("jobList", jobList);
        //输出结果
        response.getWriter().print(JSON.toJSONString(map));
    }

    //doPost方法是用于接收Post请求的
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用了上面的doGet，目的是方便操作【不管是get、post都可以在doGet中编写处理代码】
        doGet(request, response);
    }
}
